﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using MySql.Data;
using MySql.Data.MySqlClient;


namespace Safe_Driving
{
    public class ConnexionManager
    {
        private string _server = "localhost";
        private string _user = "root";
        private string _database = "safe_driving";
        private string _port = "3306";
        private string _password = String.Empty;

        # region Accesseurs
        public string Server
        {
            get
            {
                return _server;
            }
        }

        public string User
        {
            get
            {
                return _user;
            }
        }

        public string Database
        {
            get
            {
                return _database;
            }
        }

        public string Port
        {
            get
            {
                return _port;
            }
        }

        public string Password
        {
            get
            {
                return _password;
            }
        }
        #endregion


        /// <summary>
        /// Permet d'exécuter une requête DDL (type SELECT).
        /// </summary>
        /// <param name="requete">La requête SQL à exécuter.</param>
        /// <returns>Une datatable contenant le jeu de données récupérées depuis la BDD.</returns>
        public DataTable ExecuteQueryDDL(string requete)
        {
            DataTable dtResults = new DataTable();
            
            string connStr = "server="+this.Password+";user="+this.User+";database="+this.Database+";port="+this.Port+";password="+this.Password+";";
            MySqlConnection conn = new MySqlConnection(connStr);
            try
            {
                conn.Open();

                MySqlCommand cmd = new MySqlCommand(requete, conn);
                MySqlDataReader rdr = cmd.ExecuteReader();
                dtResults.Load(rdr);
                rdr.Close();
                
                return dtResults;
            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                conn.Close();
            }
        }


        /// <summary>
        /// Permet d'exécuter une requête DML (INSERT, UPDATE, DELETE)
        /// </summary>
        /// <param name="requete">La requête SQL à exécuter.</param>
        /// <returns>Retourne "vrai" si la requête s'est bien exécutée, "faux" sinon.</returns>
        public bool ExecuteQueryDML(string requete)
        {
            string connStr = "server="+this.Password+";user="+this.User+";database="+this.Database+";port="+this.Port+";password="+this.Password+";";
            MySqlConnection conn = new MySqlConnection(connStr);

            MySqlCommand cmd = new MySqlCommand();
            cmd.CommandText = requete;
 
            try
            {
                cmd.Connection = conn;
                conn.Open();
                MySqlDataReader executer = cmd.ExecuteReader();
                executer.Close();

                return true;
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
                conn.Close();
            }
        }
    }
}
